﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;
using System.Data.SqlClient;
public partial class Doanh_Nghiệp_DoanhNghiep_MatHang : System.Web.UI.Page
{
    public int madn;

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["madn"] == "")
        //{
        //Response.Redirect("~/doanh nghiệp/DN_dangnhap.aspx");
        //}
        if (!IsPostBack)
        {
            Loaddata();
            LoadDropDownList();
            
        }
    }
    public Object soluonghinh(int mamhang)
    {

        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select count(mamh) from hinhanh where mamh='" + mamhang + "'", cn);
        return cmd.ExecuteScalar();


    }
    private void LoadDropDownList()
    {


        LoaiHangColection loaihang = LOAIHANG_BUS.GetAll();
        ddl_LoaiHang.DataTextField = LOAIHANGCOLUMNS.TenLoai.ToString();
        ddl_LoaiHang.DataValueField = LOAIHANGCOLUMNS.MaLoai.ToString();
        ddl_LoaiHang.DataSource = loaihang;
        ddl_LoaiHang.DataBind();
        ddl_LoaiHang.Items.Insert(0, new ListItem("All", ""));



        TinhTrang_SanPhamCollection tinhtrang_sp = TinhTrang_SanPham_BUS.GetAll();
        ddl_TinhTrang.DataTextField = TinhTrang_SanPhamColumns.TenTinhTrang.ToString();
        ddl_TinhTrang.DataValueField = TinhTrang_SanPhamColumns.MaTinhTrang_SP.ToString();
        ddl_TinhTrang.DataSource = tinhtrang_sp;
        ddl_TinhTrang.DataBind();
        ddl_TinhTrang.Items.Insert(0, new ListItem("All", ""));

    }
    private void Loaddata()
    {
        try
        {
            Criteria cri = new Criteria();
            int total = 0;

            madn = int.Parse(Session["madn"].ToString());
            if (ddl_LoaiHang.SelectedValue != "")
            {
                cri.add(Condition.AND, LOAIHANGCOLUMNS.MaLoai, Condition.EQUAL, int.Parse(ddl_LoaiHang.SelectedValue));
            }
            if (ddl_TinhTrang.SelectedValue != "")
            {
                cri.add(Condition.AND, TinhTrang_SanPhamColumns.MaTinhTrang_SP, Condition.EQUAL, int.Parse(ddl_TinhTrang.SelectedValue));
            }
            if (txttenmathang.Text != "")
            {
                cri.add(Condition.AND, MatHangColumns.TenMH, Condition.LIKE, txttenmathang.Text);
            }
            ptmathang.PageSize = 8;
            // điều kiện tìm kiếm
            cri.add(Condition.AND, MatHangColumns.MaDN.ToString(), Condition.EQUAL, madn);
            MatHangColection mathang = MATHANG_BUS.Get(cri.Criter, ptmathang.CurrentIndex, ptmathang.PageSize, MatHangColumns.MaMH.ToString(), OrderDirection.ASC.ToString(), out total);
            Repeater1.DataSource = mathang;
            Repeater1.DataBind();
            ptmathang.ItemCount = total;
        }
        catch
        {
            Response.Redirect(Request.RawUrl);
        }
        
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            MatHang_DTO sanPham = (MatHang_DTO)e.Item.DataItem;
            HyperLink hyp_TenSP = (HyperLink)e.Item.FindControl("hyp_TenSP");
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hyl_sua");

            hyp_TenSP.NavigateUrl = "~/doanh nghiệp/Doanhnghiep_chitietmathang.aspx?id=" + sanPham.MaMH.ToString();
            #region"Loại Hàng"
            Literal lit_MaLoai = (Literal)e.Item.FindControl("l_loaihang");
            LoaiHang_DTO LoaiSP = LOAIHANG_BUS.GetByID(sanPham.MaLoai);
            lit_MaLoai.Text = LoaiSP.TenLoai;

            #endregion
            #region"tinh trang"
            Literal mota = (Literal)e.Item.FindControl("l_MaTinhTrang");
            TinhTrang_SanPham_DTO ttsanpham = TinhTrang_SanPham_BUS.GetByID(sanPham.MaTinhTrang_SP);
            mota.Text = ttsanpham.TenTinhTrang;
              #endregion
            #region"hinh anh"
            HinhAnh_DTO hinhminhhoa = HinhAnh_BUS.GetByID(sanPham.MaMH);
            Image hinhanh = (Image)e.Item.FindControl("i_HinhMinhHoa");
            int soluongimage = int.Parse(soluonghinh(sanPham.MaMH).ToString());
            if (soluongimage <= 0)
            {
                hinhanh.ImageUrl = "~/photos/thumb/" + "no_images.jpg";
            }
            else
            {
                hinhanh.ImageUrl = "~/photos/thumb/" + hinhminhhoa.Hinh;
            }

            #endregion
            #region'lấy tên mặt hàng"
            /////// lay ten mat hang
            string input = sanPham.TenMH.ToString();
            int vitri = 0;
            input = input.Trim(); // xóa các khoảng trắng vô nghĩ ở 2 đầu
            int count = 1;
            for (int i = 1; i < input.Length; i++)
            {
                vitri++;

                if (input[i] == ' ' && input[i - 1] != ' ')
                    count++;
                if (count == 5 || count > 5)
                {
                    //hyp_TenSP.Text = sanPham.TenMH;
                    hyp_TenSP.Text = input.Substring(0, vitri + 1) + "...";
                    hpl_Edit.NavigateUrl = "~/doanh nghiệp/Doanhnghiep_mathang_update.aspx?id=" + sanPham.MaMH.ToString();
                    //FreeTextBox1.Text = input.Substring(0, vitri + 1);
                    return;
                }
                else
                {
                    hyp_TenSP.Text = sanPham.TenMH;
                    hpl_Edit.NavigateUrl = "~/doanh nghiệp/Doanhnghiep_mathang_update.aspx?id=" + sanPham.MaMH.ToString();
                }
                
               

            }
           
           
            #endregion

          



          
            //if (ttsanpham.MaTinhTrang_SP == 6)
            //{
            //    Session["opendeal"] ="opendeal";
                
            //}
            //else
            //{
            //    Session["opendeal"] = "";
            //}

          

        }

    }
    protected void ptmathang_Command(object sender, CommandEventArgs e)
    {
        ptmathang.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        Loaddata();
    }
    protected void bttimkiem_Click(object sender, EventArgs e)
    {
        Loaddata();
    }
}
